package com.jiantong.web.open.app.user;

import com.google.common.base.Throwables;
import com.jiantong.user.model.Contact;
import com.jiantong.user.service.ContactReadService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Response;
import io.terminus.common.utils.Splitters;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * Author:cp
 * Created on 9/12/16.
 */
@OpenBean
@Slf4j
public class ContactApi {

    @RpcConsumer
    private ContactReadService contactReadService;


    @OpenMethod(key = "get.user.contacts", paramNames = {"ownerId"}, httpMethods = RequestMethod.GET)
    public List<Contact> getUserContact(Long ownerId) {

        Response<List<Contact>> response = contactReadService.findByOwnerId(ownerId);
        if(!response.isSuccess()){
            log.error("find contact by owner id:{} fail,error:{}",ownerId,response.getError());
            throw new OPServerException(response.getError());
        }

        return response.getResult();
    }


    private List<Long> transformOrderIds(String orderIds) {
        try {
            return Splitters.splitToLong(orderIds, Splitters.COMMA);
        } catch (Exception e) {
            log.error("fail to transform {} to orderIds,cause:{}",
                    orderIds, Throwables.getStackTraceAsString(e));
            throw new OPClientException("illegal.order.ids");
        }
    }

}
